package com.wct.genetic;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WEI
 * @date 2018/6/5 22:03
 */
public class CityTool {

    public static double getCityDis(City c1, City c2) {

        return Math.sqrt((c1.getX() - c2.getX()) * (c1.getX() - c2.getX()) + (c1.getY() - c2.getY()) * (c1.getY() - c2.getY()));
    }

    public static List<City> getCityList(String cityFile) {

        List<City> cityList = new ArrayList<City>();
        try {
            // read file content from file
            FileReader reader = new FileReader(cityFile);
            BufferedReader br = new BufferedReader(reader);
            String str;
            
            while ((str = br.readLine()) != null) {
                String[] weight = str.split(" ");
                
                int cityNum = Integer.valueOf(weight[0]);
                double xInt = Double.valueOf(weight[1]);
                double yInt = Double.valueOf(weight[2]);
                
                City tspCity = new City(cityNum, xInt, yInt);
                cityList.add(tspCity);
            }
            br.close();
            reader.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cityList;
    }

    public static int[][] getCityDisMatrix(List<City> cityList){
    	int cityCount = cityList.size();
    	int[][] disMatrix = new int[cityCount][cityCount];
    	for (int i = 0; i < cityCount; i++) {
			disMatrix[i][i] = 0;
    		for (int j = 0; j < cityCount; j++) {
				if (i != j){
					disMatrix[i][j] = (int) getCityDis(cityList.get(i), cityList.get(j));
				}
			}
		}
        return disMatrix;
    }
}
